package com.ahyesyesteam.aicarfaultrepair.controller;


import com.ahyesyesteam.aicarfaultrepair.pojo.Solution;
import com.ahyesyesteam.aicarfaultrepair.service.ISolutionService;
import com.ahyesyesteam.aicarfaultrepair.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author SSQ
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/solutions")
public class SolutionController {

    @Autowired
    private ISolutionService solutionService;

    @GetMapping("/detail/{id}")
    public Response showDetail(@PathVariable("id") Long solutionId){
        solutionService.updateViewCount(solutionId);
        return Response.ok().put("data",solutionService.getById(solutionId));
    }

    @GetMapping("/hotSolutionList")
    public Response showHotSolutionList(){
        int limit = 4;
        return Response.ok().put("data",solutionService.getHotList(limit));
    }


    @GetMapping("/like/{solution_id}")
    public Response likeSolution(@PathVariable("solution_id") Long solutionId){
        solutionService.likeSolution(solutionId);
        return Response.ok();
    }

    @RequestMapping("/list")
    public Response list(@RequestParam(value = "page", defaultValue = "0") int page,
                         @RequestParam(value = "size", defaultValue = "5") int size,
                         @RequestParam(value = "key",defaultValue = "")String key) {
        List<Solution> solutionList = solutionService.selectSolutionList(page,size,key);
        return Response.ok().put("page", solutionList);
    }
}
